<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
   	//后台页面
   	public function getIndex(Request $request)
   	{
   		//echo '我是首页<a href="./admin/add">我是添加</a>';
         //查询所有的数据
         // $users = DB::table('user')->get();
         //$users = DB::table('user')->paginate(5);

         //获取分页大写
         $num = $request->input('num',10);
         if($request->input('keywords')){
            $users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
         }else{
            $users = DB::table('user')->paginate($num);
         }
         $data = $request->all();
 

         return view('admin.user.index',['users'=>$users,'request'=>$data]); 
   	}

   	//添加
   	public function getAdd()
   	{
   		return view('admin.user.add');
   	}

      //执行添加
      public function postInsert(Request $request)
      {

         //查看数据
         //dd($request->all());
         //执行数据检测
          // if(!$request->input('username'))
          // {
          //    return back()->with('error','yong');
          // }
          //dd($request->all());
            
            //数据验证
            $this->validate($request,[
               'username'=>'required|max:25',
               'password'=>'required',
               'repassword'=>'required|same:password',
               'age'=>'required',
               'phone'=>'required|regex:[1\d{10}]',
               'pic'=>'required',
               ],[
               'username.required'=>'用户名不能为空',
               'username.max'=>'用户名太长',
               'password.required'=>'用户密码不能为空',
               'repassword.required'=>'确认密码不能为空',
               'repassword.same'=>'两次密码不一致',
               'age.required'=>'年龄不能为空',
               'phone.required'=>'手机号不能为空',
               'phone.regex'=>'手机号格式不正确',
               'pic.required'=>'头像不能为空',
               ]);

               

            //验证完成后提取全部数据
            $data = $request->only(['username','password','age','phone','pic']);
            $data['token'] = str_random(50);
            $data['password'] = Hash::make($data['password']);

            if($request->hasFile('pic')){
               //随机文件名
                  $name = md5(time()+rand(1,999));
               //获取后缀名
                  $su = $request->file('pic')->getClientOriginalExtension();
                  // dd($su);jpg png jpeg gif tiff
                  $arr = array('jpg','png','jpeg','tiff');
               //判断
                  if(!in_array($su,$arr)){
                  // die('非法上传');
                  echo '<script type="text/javascript">
                        alert("上传文件不合法");
                        location.href="http://niuwan.com/admin/user/add";
                        </script>';
                  }
               //将文件移动到指定目录下
               $request->file('pic')->move('./uploads/', $name.'.'.$su);
               //echo '<img src="/uploads/'.$name.'.jpg" alt="" width="50" height="50">';
               $data['pic'] =  $name.'.'.$su;
            }
            //执行数据插入
            $res = DB::table('user')->insert($data);
            //判断是否插入成功
            if($res){
                  return redirect('/admin/user/index')->with('success','用户添加成功');
            }else{
               return back()->with('error','用户添加失败');
            }
            //dd($data);
      }

      //修改

      public function getEdit($id)
      {
         //echo $id;
         //根据用户id查询
         $user = DB::table('user')->where('id',$id)->first();
         //解析模板
         return view('admin.user.edit',['user'=>$user]);
      }

      public function postUpdate(Request $request)
      {
         $id = $request->only(['id']);
         //echo $id;
         $data = $request->only(['username','age','phone']);
         //dd($data);
         //执行修改
         $res = DB::table('user')->where('id',$id)->update($data);
         //dd($res);
          //判断是否修改成功
            if($res){
               return redirect('/admin/user/index')->with('success','用户修改成功');
            }else{
               return back()->with('error','用户修改失败');
            }
      }

      //删除
       public function getDelete($id)
       {
         //dd($id);
         //执行
         //$user = DB::table('user')->where('id',$id)->first();

         $res = DB::table('user')->where('id',$id)->delete();
         //dd($res);
         if($res){
               return redirect('/admin/user/index')->with('success','用户删除成功');
            }else{
               return back()->with('error','用户删除失败');
            }


       }
}
